// const electron = require('electron');
// const ipcRenderer = electron.ipcRenderer;
const {IPCMainEventsPlay} = require('../../../../play/ipc-events');

const {
    CommandTargetType
} = require('./command');

const {
    Selected
} = require('./selected');
const {
    Keymap,
    KeymapHelper
} = require('../../../js/keymap-helper');
const {
    EditorType
} = require('./config');

const {
    MusicScoreYinFuContentType
} = require('../../music-score/js/music-score-yin-fu-content');

const {RightKeyClickType, MusicScoreType} = require('../../music-score/js/music-score-const');
const {NoteHeadAttrList} = require('../../composing-music/js/MusicNoteHeadData');
const {SectionLineList} = require('../../composing-music/js/MusicSectionData');

class Editor {

    static className() {
        return 'Editor';
    }

    constructor(editorShared) {
        this._editorShared = editorShared;
        editorShared._editor = this;
        this.scale = 1;
        this.data = null;
        this.initialKeymapHelper();
    }

    init() {
        this.history.onApply = this.onApplyHandler;
        this.history.onUnapply = this.onUnapplyHandler;
    }

    initialKeymapHelper() {
        let _this = this;
        if (shared.editorManager.editorType === EditorType.MusicScoreEditor || shared.editorManager.editorType === EditorType.MusicScorePlayer || shared.editorManager.editorType === EditorType.MusicEducation) {
            this.keymapHelper.on(Keymap.UndoKey, () => {
                shared.editorManager.currentEditor === _this && _this.undo();
            });
            this.keymapHelper.on(Keymap.RedoKey, () => {
                shared.editorManager.currentEditor === _this && _this.redo();
            });
            this.keymapHelper.on(Keymap.Delete, () => {
                shared.editorManager.currentEditor === _this && shared.editorManager.currentEditor.currentSelected.removeCurrentSelectedObject();
            });
        }
        if (shared.editorManager.editorType === EditorType.MusicScoreEditor) {
            this.keymapHelper.on(Keymap.Space, () => {
                if (shared.editorManager.currentSelectedObjectInfo) {
                    shared.editorManager.currentSelectedObjectInfo.parent.isKeymapSpace = true;
                    shared.editorManager.currentSelectedObjectInfo.parent.isKeymapBackspace = false;
                }
            });

            this.keymapHelper.on(Keymap.BackSpace, () => {
                let currentSelected = shared.editorManager.currentSelectedObjectInfo;
                if (currentSelected && currentSelected.target.className === 'MusicScoreGeCiBox') {
                    let current = currentSelected.target.getVUEComponent();
                    let danPuBiao = currentSelected.parent;
                    danPuBiao.isKeymapBackspace = true;
                    // setTimeout(() => {
                    //     current.collapse(-1);
                    // }, 10);
                }
            });

            this.keymapHelper.on(Keymap.Enter, () => {
                if (!shared.editorManager.isPlaying) {
                    let currentSelected = shared.editorManager.currentSelectedObjectInfo;
                    if (currentSelected && currentSelected.target.className === 'MusicScoreGeCiBox') {
                        let current = currentSelected.target.getVUEComponent();
                        let danPuBiao = currentSelected.parent;
                        danPuBiao.isKeymapSpace = true;
                        danPuBiao.isKeymapEnter = true;
                        danPuBiao.isKeymapBackspace = false;
                        setTimeout(() => {
                            current.collapse(-1);
                        }, 10);
                    } else {
                        let index = shared.editorManager.currentMusicScoreData.fuPuBiaoArray.length;
                        if (currentSelected && currentSelected.target && currentSelected.target.index && Object.isNotNullOrUndefinedOrNaN(currentSelected.target.index.fuPuBiaoIndex)) {
                            index = currentSelected.target.index.fuPuBiaoIndex + 1;
                        }
                        this.editorShared.editor.musicScoreEditor.insertFuPuBiao(index);
                    }
                }
            });
            this.keymapHelper.on(Keymap.Up, () => {
                let currentData = shared.editorManager.currentSelectedObjectInfo.target;
                if (currentData.className === "MusicScoreYinFu" || currentData.className === "MusicScoreYinFuContent") {
                    this.changeMusicScoreYinFuValues(Keymap.Up);
                }
            });
            this.keymapHelper.on(Keymap.Down, () => {
                let currentData = shared.editorManager.currentSelectedObjectInfo.target;
                if (currentData.className === "MusicScoreYinFu" || currentData.className === "MusicScoreYinFuContent") {
                    this.changeMusicScoreYinFuValues(Keymap.Down);
                }
            });
            // 字母输入音符
            this.keymapHelper.on(Keymap.Alphabet, (event) => {
                if (shared.editorManager.currentSelectedObjectInfo) {
                    let currentData = shared.editorManager.currentSelectedObjectInfo.target;
                    if (currentData.className === "MusicScoreXiaoJie") {
                        currentData.getVUEComponent() && currentData.getVUEComponent().insertYinFu(event.keyCode);
                    }
                }
            });
            // 键盘上下左右键输入
            this.keymapHelper.on(Keymap.DirectionKey, (event) => {
                if (event.keyCode === 38 || event.keyCode === 40) {
                    this.changeMusicScoreYinFuValue(event.keyCode);
                } else {
                    this.changeMusicScoreIndex(event.keyCode);
                }
            });
            // 插入小节
            this.keymapHelper.on(Keymap.Slant, () => {
                if (shared.editorManager.currentSelectedObjectInfo && shared.editorManager.currentSelectedObjectInfo.target) {
                    let index = shared.editorManager.currentSelectedObjectInfo.target.index.clone();
                    if (_this.currentSelected.selectedObjectClassName === 'MusicScoreXiaoJie') {
                        index.xiaoJieIndex++;
                    } else if (_this.currentSelected.selectedObjectClassName === 'MusicScoreDanPuBiao') {
                        index.xiaoJieIndex = shared.editorManager.currentSelectedObjectInfo.target.xiaoJieArray.length;
                    } else if (_this.currentSelected.selectedObjectClassName === 'MusicScoreFuPuBiao') {
                        index.xiaoJieIndex = shared.editorManager.currentSelectedObjectInfo.target.danPuBiaoArray.firstObject().xiaoJieArray.length;
                    }
                    shared.editorManager.currentMusicScoreEditor.insertXiaoJie(index);
                }
            });
            // 数字键输入属性
            this.keymapHelper.on(Keymap.Number, (event) => {
                if (_this.currentSelected.selectedObjectClassName === 'MusicScoreXiaoJie') {
                    let currentData = shared.editorManager.currentSelectedObjectInfo.target;
                    if (currentData.getVUEComponent() && event.keyCode < 56 && event.keyCode > 48) {
                        currentData.getVUEComponent().insertYinFu(event.keyCode);
                    }
                } else {
                    this.rightKeyNumClick(event.keyCode);
                }
            });
            // 复制粘贴
            this.keymapHelper.on(Keymap.Copy, (event) => {
                if (shared.editorManager.editorType === EditorType.MusicScoreEditor && _this.currentSelected.items.length
                    && (_this.currentSelected.selectedObjectClassName === 'MusicScoreXiaoJie' || _this.currentSelected.selectedObjectClassName === 'MusicScoreYinFu' || _this.currentSelected.selectedObjectClassName === 'MusicScoreYinFuContent')) {
                    shared.editorManager.pasteboard.setContent(_this.currentSelected.items);
                    shared.editorManager.pasteboard.type = _this.currentSelected.selectedObjectClassName;
                }
            });
            this.keymapHelper.on(Keymap.Cut, () => {
                if (shared.editorManager.editorType === EditorType.MusicScoreEditor && _this.currentSelected.items.length
                    && (_this.currentSelected.selectedObjectClassName === 'MusicScoreYinFu')) {
                    shared.editorManager.pasteboard.setContent(_this.currentSelected.items);
                    shared.editorManager.pasteboard.type = _this.currentSelected.selectedObjectClassName;
                    shared.editorManager.currentEditor === _this && shared.editorManager.currentEditor.currentSelected.removeCurrentSelectedObject();
                }
            });
            this.keymapHelper.on(Keymap.Past, () => {
                if (shared.editorManager.pasteboard.type === 'MusicScoreXiaoJie') {
                    if (shared.editorManager.editorType === EditorType.MusicScoreEditor
                        && (_this.currentSelected.selectedObjectClassName === 'MusicScoreFuPuBiao'
                            || _this.currentSelected.selectedObjectClassName === 'MusicScoreDanPuBiao'
                            || _this.currentSelected.selectedObjectClassName === 'MusicScoreXiaoJie')) {
                        _this.musicScoreEditor.insertXiaoJie(_this.currentSelected.currentSelectedObjectInfo.target.index, shared.editorManager.pasteboard.content);
                    }
                } else if (shared.editorManager.pasteboard.type === 'MusicScoreYinFu' || shared.editorManager.pasteboard.type === 'MusicScoreYinFuContent') {
                    let currentSelectedInfo = shared.editorManager.currentSelectedObjectInfo;
                    if (currentSelectedInfo && currentSelectedInfo.target && currentSelectedInfo.target.className === 'MusicScoreXiaoJie') {
                        let index = currentSelectedInfo.target.index.clone();
                        index.shengBuIndex = currentSelectedInfo.target.getVUEComponent().insertShengBuIndexNumber - 1;
                        _this.musicScoreEditor.insertPastYinFu(shared.editorManager.pasteboard.content, index);
                    }
                }
            });

            //快捷键添加符号
            this.keymapHelper.on(Keymap.Symbols, (event) => {
                let currentSelectedInfo = shared.editorManager.currentSelectedObjectInfo;
                if (currentSelectedInfo) {
                    let currentMusicScoreEditor = shared.editorManager.currentMusicScoreEditor;
                    let noteAttrList = shared.NoteAttrList;
                    // console.log(currentSelectedInfo);
                    if (event.keyCode === 76 && currentSelectedInfo.target.className === "MusicScoreYinFuContent") { //L键连音线
                        currentMusicScoreEditor.insertMusicScoreLianYinXianForMusicScoreYinFuContent(currentSelectedInfo.target);
                    } else if (event.keyCode === 85 && currentSelectedInfo.target.className === "MusicScoreYinFu") { //U高八度记号
                        let symbolItem = noteAttrList[noteAttrList.map(item => item.name).indexOf('八度')];
                        currentMusicScoreEditor.insertBaDu(symbolItem);
                    } else if (event.keyCode === 73 && currentSelectedInfo.target.className === "MusicScoreYinFu") {   //I低八度记号
                        let symbolItem = noteAttrList[noteAttrList.map(item => item.name).indexOf('八度低')];
                        currentMusicScoreEditor.insertBaDu(symbolItem);
                    } else if (event.keyCode === 79 && currentSelectedInfo.target.className === "MusicScoreXiaoJie") { //O 反复记号（前）
                        currentMusicScoreEditor.insertXiaoJieXiaoJieXian(SectionLineList[1]);
                    } else if (event.keyCode === 80 && currentSelectedInfo.target.className === "MusicScoreXiaoJie") { //P 反复记号（后）
                        currentMusicScoreEditor.insertXiaoJieXiaoJieXian(SectionLineList[2]);
                    } else if (event.keyCode === 78 && currentSelectedInfo.target.className === "MusicScoreYinFu") { // N 踏板开始
                        let symbolItem = noteAttrList[noteAttrList.map(item => item.name).indexOf('踏板')];
                        currentMusicScoreEditor.insertTaBan(symbolItem.contentList[6]);
                    } else if (event.keyCode === 89 && currentSelectedInfo.target.className === "MusicScoreYinFu") { // Y 翻转
                        currentMusicScoreEditor.updateYinFuDirection();
                    } else if (event.keyCode === 74 && currentSelectedInfo.target.className === "MusicScoreYinFu") { // J 渐强记号
                        let symbolItem = noteAttrList[noteAttrList.map(item => item.name).indexOf('渐强')];
                        currentMusicScoreEditor.insertQiangRuo(symbolItem);
                    } else if (event.keyCode === 75 && currentSelectedInfo.target.className === "MusicScoreYinFu") { // K 渐弱记号
                        let symbolItem = noteAttrList[noteAttrList.map(item => item.name).indexOf('渐弱')];
                        currentMusicScoreEditor.insertQiangRuo(symbolItem);
                    } else if (event.keyCode === 83 && currentSelectedInfo.target.className === "MusicScoreYinFu") { // S 三连音
                        currentMusicScoreEditor.insertMusicScoreLianYin(currentSelectedInfo.target, 3)
                    } else if (event.keyCode === 84 && currentSelectedInfo.target.className === "MusicScoreYinFu") { // T 文本输入框
                        // this.$emit(YinFuClickTypeMap.CaptionText)
                    } else if (event.keyCode === 96 && currentSelectedInfo.target.className === "MusicScoreYinFu") { // 小键盘 0 保持音
                        let symbolItem = noteAttrList[noteAttrList.map(item => item.name).indexOf('保持音')];
                        currentMusicScoreEditor.insertDefaultYinFuSymbols(symbolItem);
                    } else if (event.keyCode === 97 && currentSelectedInfo.target.className === "MusicScoreYinFuContent") { // 小键盘 1 升记号
                        let symbolItem = NoteHeadAttrList[NoteHeadAttrList.map(item => item.name).indexOf('升记号')];
                        currentMusicScoreEditor.insertBianHuaYin(symbolItem);
                    } else if (event.keyCode === 98 && currentSelectedInfo.target.className === "MusicScoreYinFuContent") { // 小键盘 2 降记号
                        let symbolItem = NoteHeadAttrList[NoteHeadAttrList.map(item => item.name).indexOf('降记号')];
                        currentMusicScoreEditor.insertBianHuaYin(symbolItem);
                    } else if (event.keyCode === 99 && currentSelectedInfo.target.className === "MusicScoreYinFuContent") { // 小键盘 3 还原记号
                        let symbolItem = NoteHeadAttrList[NoteHeadAttrList.map(item => item.name).indexOf('还原记号')];
                        currentMusicScoreEditor.insertBianHuaYin(symbolItem);
                    } else if (event.keyCode === 100 && currentSelectedInfo.target.className === "MusicScoreYinFu") { // 小键盘 4 PP记号
                        let symbolItem = noteAttrList[noteAttrList.map(item => item.name).indexOf('很弱')];
                        currentMusicScoreEditor.insertDefaultYinFuSymbols(symbolItem);
                    } else if (event.keyCode === 101 && currentSelectedInfo.target.className === "MusicScoreYinFu") { // 小键盘 5 P记号
                        let symbolItem = noteAttrList[noteAttrList.map(item => item.name).indexOf('弱')];
                        currentMusicScoreEditor.insertDefaultYinFuSymbols(symbolItem);
                    } else if (event.keyCode === 102 && currentSelectedInfo.target.className === "MusicScoreYinFu") { // 小键盘 6 mP记号
                        let symbolItem = noteAttrList[noteAttrList.map(item => item.name).indexOf('中弱')];
                        currentMusicScoreEditor.insertDefaultYinFuSymbols(symbolItem);
                    } else if (event.keyCode === 103 && currentSelectedInfo.target.className === "MusicScoreYinFu") { // 小键盘 7 mf记号
                        let symbolItem = noteAttrList[noteAttrList.map(item => item.name).indexOf('中强')];
                        currentMusicScoreEditor.insertDefaultYinFuSymbols(symbolItem);
                    } else if (event.keyCode === 104 && currentSelectedInfo.target.className === "MusicScoreYinFu") { // 小键盘 8 f记号
                        let symbolItem = noteAttrList[noteAttrList.map(item => item.name).indexOf('强')];
                        currentMusicScoreEditor.insertDefaultYinFuSymbols(symbolItem);
                    } else if (event.keyCode === 105 && currentSelectedInfo.target.className === "MusicScoreYinFu") { // 小键盘 9 ff记号
                        let symbolItem = noteAttrList[noteAttrList.map(item => item.name).indexOf('很强')];
                        currentMusicScoreEditor.insertDefaultYinFuSymbols(symbolItem);
                    } else if (event.keyCode === 107 && currentSelectedInfo.target.className === "MusicScoreYinFu") { // 小键盘 + SF记号
                        let symbolItem = noteAttrList[noteAttrList.map(item => item.name).indexOf('极强')];
                        currentMusicScoreEditor.insertDefaultYinFuSymbols(symbolItem);
                    } else if (event.keyCode === 106 && currentSelectedInfo.target.className === "MusicScoreYinFu") { // 小键盘 * 重音记号
                        let symbolItem = noteAttrList[noteAttrList.map(item => item.name).indexOf('重音记号')];
                        currentMusicScoreEditor.insertDefaultYinFuSymbols(symbolItem);
                    } else if (event.keyCode === 109 && currentSelectedInfo.target.className === "MusicScoreYinFu") { // 小键盘 - 顿音记号
                        let symbolItem = noteAttrList[noteAttrList.map(item => item.name).indexOf('顿音')];
                        currentMusicScoreEditor.insertDefaultYinFuSymbols(symbolItem);
                    } else if (event.keyCode === 110 && currentSelectedInfo.target.className === "MusicScoreYinFu") { // 小键盘 . 延长音记号
                        let symbolItem = noteAttrList[noteAttrList.map(item => item.name).indexOf('延长音')];
                        currentMusicScoreEditor.insertDefaultYinFuSymbols(symbolItem);
                    } else if (event.keyCode === 111 && currentSelectedInfo.target.className === "MusicScoreYinFu") { // 小键盘 / 跳音记号
                        let symbolItem = noteAttrList[noteAttrList.map(item => item.name).indexOf('长顿音')];
                        currentMusicScoreEditor.insertDefaultYinFuSymbols(symbolItem);
                    }
                }
            });

            //监测钢琴键盘事件 钢琴键盘打谱
            ipcRenderer.on(IPCMainEventsPlay.PlayNote, (event, midiMessage) => {
                if (shared.editorManager.currentSelectedObjectInfo) {
                    let currentData = shared.editorManager.currentSelectedObjectInfo.target;
                    if (currentData.className === "MusicScoreXiaoJie") {
                        currentData.getVUEComponent() && currentData.getVUEComponent().insertYinFuFormMidi(midiMessage.midi);
                    }
                }
            });
        }
    }

    // 上移八度 下移八度 方法
    changeMusicScoreYinFuValues(keymap) {
        let currentSelectData = shared.editorManager.currentEditor.currentSelected.currentSelectedObjectInfo.target;
        this.originalYinFuContentsValue = {};
        this.targetYinFuContentsValue = {};
        let newValue = keymap === Keymap.Up ? -3.5 : 3.5;
        // 休止符不处理移动八度
        if (currentSelectData.type === MusicScoreYinFuContentType.XiuZhiFu) return;
        if (currentSelectData.className === "MusicScoreYinFuContent") {
            this.originalYinFuContentsValue[currentSelectData.id] = {value: currentSelectData.value};
            this.targetYinFuContentsValue[currentSelectData.id] = {value: currentSelectData.value + newValue};
            currentSelectData = shared.editorManager.currentEditor.currentSelected.currentSelectedObjectInfo.parent;
        } else {
            currentSelectData.yinFuContents.forEach(musicScoreYinFuContent => {
                this.originalYinFuContentsValue[musicScoreYinFuContent.id] = {value: musicScoreYinFuContent.value};
                this.targetYinFuContentsValue[musicScoreYinFuContent.id] = {value: musicScoreYinFuContent.value + newValue};
            });
        }
        shared.editorManager.currentMusicScoreEditor.updateYinFuContentsValues(currentSelectData, this.originalYinFuContentsValue, this.targetYinFuContentsValue);
    }

    // 向上 向下按键
    changeMusicScoreYinFuValue(keyCode) {
        let currentSelectData = shared.editorManager.currentEditor.currentSelected.currentSelectedObjectInfo;
        let musicScoreYinFu = currentSelectData.target;
        if (musicScoreYinFu && (musicScoreYinFu.className === 'MusicScoreYinFuContent' || musicScoreYinFu.className === 'MusicScoreYinFu')) {
            this.originalYinFuContentsValue = {};
            this.targetYinFuContentsValue = {};
            // 全休止符/二分休止符不能移动半格，必须移动一整格
            let moveValue = (musicScoreYinFu.type === MusicScoreYinFuContentType.XiuZhiFu && currentSelectData.parent && currentSelectData.parent.yinChang >= 128) ? 1 : 0.5;
            let newValue = keyCode === 38 ? -moveValue : moveValue;
            if (musicScoreYinFu.className === "MusicScoreYinFuContent") {
                this.originalYinFuContentsValue[musicScoreYinFu.id] = {value: musicScoreYinFu.value};
                this.targetYinFuContentsValue[musicScoreYinFu.id] = {value: musicScoreYinFu.value + newValue};
                musicScoreYinFu = shared.editorManager.currentEditor.currentSelected.currentSelectedObjectInfo.parent;
            } else {
                musicScoreYinFu.yinFuContents.forEach(musicScoreYinFuContent => {
                    this.originalYinFuContentsValue[musicScoreYinFuContent.id] = {value: musicScoreYinFuContent.value};
                    this.targetYinFuContentsValue[musicScoreYinFuContent.id] = {value: musicScoreYinFuContent.value + newValue};
                });
            }

            shared.editorManager.currentMusicScoreEditor.updateYinFuContentsValues(musicScoreYinFu, this.originalYinFuContentsValue, this.targetYinFuContentsValue);
        }

    }

    // 向左 向右按键
    changeMusicScoreIndex(keyCode) {
        if (shared.editorManager.currentSelectedObjectInfo) {
            let musicScoreData = shared.editorManager.currentMusicScoreData;
            let xiaoJieYinFuArrayWithMusicScoreData = musicScoreData.xiaoJieYinFuArrayWithMusicScoreData();
            let yinFuWithdanPuBiaoObject = xiaoJieYinFuArrayWithMusicScoreData.yinFuWithdanPuBiaoObject;
            let xiaoJieWithdanPuBiaoObject = xiaoJieYinFuArrayWithMusicScoreData.xiaoJieWithdanPuBiaoObject;
            let currentData = shared.editorManager.currentSelectedObjectInfo.target;
            if (currentData.className === 'MusicScoreYinFuContent') {
                currentData = shared.editorManager.currentSelectedObjectInfo.parent;
            }
            if (currentData && currentData.index) {
                let index = currentData.index;
                let danPuBiaoIndex = index.danPuBiaoIndex;
                if (currentData.className === 'MusicScoreXiaoJie') {
                    this.musicScoreClick(xiaoJieWithdanPuBiaoObject, danPuBiaoIndex, currentData.id, keyCode);
                } else if (currentData.className === 'MusicScoreYinFuContent' || currentData.className === 'MusicScoreYinFu') {
                    this.musicScoreClick(yinFuWithdanPuBiaoObject, danPuBiaoIndex, currentData.id, keyCode);
                }
            }
        }
    }

    musicScoreClick(danPuBiaoObject, danPuBiaoIndex, currentId, keyCode) {
        danPuBiaoObject[danPuBiaoIndex].forEach((xiaoJie, xiaoJieIndex) => {
            if (currentId === xiaoJie.id) {
                let musicScoreArrayIndex = keyCode === 37 ? xiaoJieIndex - 1 : xiaoJieIndex + 1;
                musicScoreArrayIndex = musicScoreArrayIndex < 0 ? 0 : musicScoreArrayIndex;
                musicScoreArrayIndex = musicScoreArrayIndex > danPuBiaoObject[danPuBiaoIndex].length - 1 ? danPuBiaoObject[danPuBiaoIndex].length - 1 : musicScoreArrayIndex;
                let musicScoreData = danPuBiaoObject[danPuBiaoIndex][musicScoreArrayIndex];
                !musicScoreData.getVUEComponent().isSelected && musicScoreData.getVUEComponent().click();
                //单独处理休止符的点击状态
                if (musicScoreData && musicScoreData.type === MusicScoreYinFuContentType.XiuZhiFu) {
                    !musicScoreData.yinFuContents[0].getVUEComponent().isSelected && musicScoreData.yinFuContents[0].getVUEComponent().click();
                }
            }
        });
    }

    //右键 键盘数字键输入
    rightKeyNumClick(keyCode) {
        let rightListData = shared.editorManager.currentEditorRightKey;
        if (rightListData) {
            let item = rightListData.list[keyCode - 49];
            switch (rightListData.clickType) {
                case RightKeyClickType.JianShiXian:
                    if (rightListData.getVUEComponent() && rightListData.getVUEComponent().noteDuration(item.value)) {
                        shared.editorManager.currentMusicScoreEditor.updateYinFuZuHe(item.value, true);
                    }
                    break;
                case RightKeyClickType.ShiZhi:
                    let currentData = shared.editorManager.currentSelectedObjectInfo.target;
                    currentData = currentData.className === 'MusicScoreYinFu' ? currentData : shared.editorManager.currentSelectedObjectInfo.parent;
                    shared.editorManager.currentMusicScoreEditor.updateYinChangForMusicScoreYinFu(currentData, item.value);
                    break;
                case RightKeyClickType.ZhiFa:
                    shared.editorManager.currentMusicScoreEditor.insertMusicScoreZhiFaForMusicScoreYinFuContent(item);
                    break;
                case RightKeyClickType.BianHuaYin:
                    shared.editorManager.currentMusicScoreEditor.insertBianHuaYin(item);
                    break;
            }
            //点击键盘删除组件
            rightListData.getVUEComponent() && rightListData.getVUEComponent().removeRightKey();
        }

    }

    static keymapHelper() {
        return KeymapHelper.sharedKeymapHelper();
    }

    get keymapHelper() {
        return this.constructor.keymapHelper();
    }

    get editorShared() {
        return this._editorShared;
    }

    get onApplyHandler() {
        if (!this._onApplyHandler) {
            let _this = this;
            this._onApplyHandler = (command) => {
                if (command.targetType === CommandTargetType.Sketchpad) {
                    _this.sketchpad.onApply(command);
                } else if (command.targetType === CommandTargetType.MusicScoreEditor) {
                    _this.musicScoreEditor.onApply(command);
                }
            };
        }
        return this._onApplyHandler;
    }

    get onUnapplyHandler() {
        if (!this._onUnapplyHandler) {
            let _this = this;
            this._onUnapplyHandler = (command) => {
                if (command.targetType === CommandTargetType.Sketchpad) {
                    _this.sketchpad.onUnapply(command);
                } else if (command.targetType === CommandTargetType.MusicScoreEditor) {
                    _this.musicScoreEditor.onUnapply(command);
                }
            };
        }
        return this._onUnapplyHandler;
    }

    /**
     * 获取元素全局唯一标识
     * @returns {number|*}
     */
    get id() {
        this.data.currentId++;
        return this.data.currentId;
    }

    get data() {
        return this._data;
    }

    set data(value) {
        let isChanged = (value && this._data !== value);
        this._data = value;
        if (isChanged) {
            this.sketchpad && (this.sketchpad.data = value.sketchpadData);
            this.musicScoreEditor && (this.musicScoreEditor.data = value.musicScoreData);
            this.init();
        }
    }

    get history() {
        return this.data.history;
    }

    /**
     * 缩放（线性缩放）
     * @param value
     */
    scaleAction(value) {
        this.scale += value;
        return this.scale;
    }

    get scale() {
        return this._scale;
    }

    set scale(value) {
        this._scale = Math.min(Math.max(value, 0.4), 2);
        this.sketchpad && (this.sketchpad.scale = this._scale);
        this.musicScoreEditor && (this.musicScoreEditor.scale = this._scale);
    }

    get sketchpad() {
        return this._sketchpad;
    }

    set sketchpad(value) {
        this._sketchpad = value;
        let _this = this;
        if (this._sketchpad) {
            this._sketchpad.getId = () => {
                return _this.id;
            };
            this._sketchpad.getHistory = () => {
                return _this.history;
            };
        }
    }

    get musicScoreEditor() {
        return this._musicScoreEditor;
    }

    set musicScoreEditor(value) {
        this._musicScoreEditor = value;
        let _this = this;
        if (this._musicScoreEditor) {
            this._musicScoreEditor.getId = () => {
                return _this.id;
            };
            this._musicScoreEditor.getHistory = () => {
                return _this.history;
            };
        }
    }

    get editable() {
        return this._editable;
    }

    set editable(value) {
        this._editable = value;
    }

    get container() {
        return this._container;
    }

    set container(value) {
        this._container = value;
        this.sketchpad && (this.sketchpad.container = value);
        this.musicScoreEditor && (this.musicScoreEditor.container = value);
    }

    get currentSelected() {
        if (!this._currentSelected) {
            this._currentSelected = new Selected();
        }
        return this._currentSelected;
    }

    /**
     * 返回
     */
    redo() {
        return this.history.redo();
    }

    /**
     * 撤销
     */
    undo() {
        return this.history.undo();
    }
}

module.exports = {
    Editor
};
